'''

尊享e生爸妈版

'''
import sys
sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests, json
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "尊享e生爸妈版"

    # 生成产品信息
    def genProductInfo(self):
        # 年龄段 61 - 65
        year = [str(i) for i in range(61, 66)]

        # 有无社保
        socialSec = ["有", "无"]

        # 保障计划 003 基础款 009 质重款
        plan = ["PACK181172564003", "PACK181180485009"]

        for v in product(year, socialSec, plan):
            # 写入数据库
            self.mysql.cursor.execute(domain[self.name]["insert_sql"].format(v[0], v[1], v[2]))

            self.mysql._conn.commit()

            print("写入%s数据库成功" % self.name)

    # 执行爬虫
    def requestJson(self, dt=None):


        # 分两种情况
        if dt["plan"] == "PACK181180485009":


            data = {"bizNo": "INST190947505212", "productCode": "PRD181143985008", "packageCode":dt["plan"],
                    "optionProjectIdList": [],
                    "premiumFactorList": [{"productPremiumFactorId": 1026019, "value": "1", "unit": "年"},
                                          {"productPremiumFactorId": 1026020, "value": "{year}-12-17".format( year = str(2019-int(dt["year"]))), "unit": ""},
                                          {"productPremiumFactorId": 1026021, "value": dt["socialSec"], "unit": ""}],
                    "marketProductCode": "PRD181143985008"}

        else:

            data = {"bizNo": "INST190947505212", "productCode": "PRD181143985008", "packageCode": dt["plan"],
                    "optionProjectIdList": [],
                    "premiumFactorList": [{"productPremiumFactorId": 1026016, "value": "1", "unit": "年"},
                                          {"productPremiumFactorId": 1026017,
                                           "value": "{year}-12-17".format(year=str(2019 - int(dt["year"]))),
                                           "unit": ""},
                                          {"productPremiumFactorId": 1026018, "value": dt["socialSec"], "unit": ""}],
                    "marketProductCode": "PRD181143985008"}


        r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=json.dumps(data))

        if r.status_code == 200:

            price = r.json()["data"]

            data = {

                "name": self.name,
                "id": dt['id'],
                "price": Helper.decimalParse(price)
            }
            self.mysql.updatePrice(data)

        else:

            data = {

                "name": self.name,
                "id": dt["id"],
                "state": 3
            }
            self.mysql.randTask(data)

    def main(self):

        while True:

            # 随机领取一条数据
            dt = self.mysql.randTask(self.name)

            if not dt:
                print("产品价格抓取完成~~~")

                return

                # 任务执行状态
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 1
            }
            self.mysql.updateTask(data)

            self.requestJson(dt)
